package org.theonept.travian.t4a.message;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

import org.theonept.travian.model.Compte;
import org.theonept.travian.model.Message;
import org.theonept.travian.t4a.R;
import org.theonept.travian.util.AsyncWeb;

public class WriteMessage extends Activity {

    private Compte compte;

    private EditText textViewDestinataire;
    private EditText textViewObject;
    private EditText textViewMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_write_message);
        // Show the Up button in the action bar.
        compte = (Compte) getIntent().getExtras().getSerializable("compte");
        setupActionBar();
        this.textViewDestinataire = (EditText) findViewById(R.id.editTextDestinataire);
        this.textViewObject = (EditText) findViewById(R.id.editTextObject);
        this.textViewMessage = (EditText) findViewById(R.id.editTextMessage);
    }

    /**
     * Set up the {@link android.app.ActionBar}.
     */
    private void setupActionBar() {

        getActionBar().setDisplayHomeAsUpEnabled(true);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.write_message, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // This ID represents the Home or Up button. In the case of this
                // activity, the Up button is shown. Use NavUtils to allow users
                // to navigate up one level in the application structure. For
                // more details, see the Navigation pattern on Android Design:
                //
                // http://developer.android.com/design/patterns/navigation.html#up-vs-back
                //
                fin();
                return true;
            case R.id.action_send_message:
                new MessageWriteAsync(compte).execute(compte);
        }
        return super.onOptionsItemSelected(item);
    }

    private void fin() {
        setResult(RESULT_OK, getIntent());
        finish();
    }

    class MessageWriteAsync extends AsyncWeb<Compte, Void, Boolean> {

        public MessageWriteAsync(Compte compte) {
            super(compte);
            // TODO Auto-generated constructor stub
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
        }

        @Override
        protected void onPostExecute(Boolean result) {
            fin();
            super.onPostExecute(result);
        }

        @Override
        protected Boolean executeWebTask(Compte... params) {
            Message.newMessage(compte, textViewDestinataire.getText()
                    .toString(), textViewObject.getText().toString(),
                    textViewMessage.getText().toString());
            return true;
        }
    }

}
